﻿using System;
using System.IO;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace Orders
{
    public class Goods
    {
        public Goods() { }
        public Goods(string goodsname, double goodsprice)
        {
            this.goodsname = goodsname;
            GoodsName = goodsname;
            this.goodsprice = goodsprice;
            GoodsPrice = goodsprice;
        }
        public string GoodsName { get; set; }
        public double GoodsPrice { get; set; }
        public override string ToString()
        {
            return "\n商品名称: " + goodsname + " 单价: " + goodsprice;
        }
        public string goodsname;
        public double goodsprice;
    }
    public class Client
    {
        public Client() { }
        public Client (string name)
        {
            this.name = name;
            Name = name;
        }
        public string Name { get; set; }
        public override string ToString()
        {
            return "\n客户姓名: " + name;
        }
        public string name;
    }
    public class OrderDetails 
    {
        public OrderDetails() { }
        public OrderDetails(Goods goods, int goodsnum)
        {
            this.goods = goods;
            Goods = goods;
            this.goodsnum = goodsnum;
            GoodsNum = goodsnum;
            this.money = goods.GoodsPrice * goodsnum;
            Money = goods.GoodsPrice * goodsnum;
        }
        public Goods Goods { get; set; }
        public int GoodsNum { get; set; }
        public double Money { get; set; }
        public override bool Equals(object obj)
        {
            OrderDetails p = obj as OrderDetails;
            if( Equals(p.goods,this.goods) && p.goodsnum ==this.goodsnum)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public override string ToString()
        {
            return "\n商品名称: " + goods.GoodsName + " 购买量: " + goodsnum + " 单价: " + goods.GoodsPrice + " 金额: " + money;
        }
        public Goods goods;
        public int goodsnum;
        public double money;
    }
    public class Order
    {
        public Order() { }
        public Order(int orderid, List<OrderDetails> items, Client client)
        {
            this.orderid = orderid;
            OrderID = orderid;
            this.items = items;
            Items = items;
            this.client = client;
            Client = client;
            this.moneysum = sum();
            MoneySum = moneysum;
        }
        public double sum()
        {
            foreach (OrderDetails item in items)
            {
                moneysum += item.Money;
            }
            return moneysum;
        }
        public int OrderID { get; set; }
        public Client Client { get; set; }
        public List<OrderDetails> Items { get; set; }
        public double MoneySum { get; set; }
        public override string ToString()
        {
            StringBuilder details = new StringBuilder();
            foreach (OrderDetails item in items)
            {
                details.Append(item.ToString());
            }
            return "\n订单编号为:" + OrderID + " 客户: " + client.Name + " 总金额: " + MoneySum + details + "\n";
        }
        public override bool Equals(object obj)
        {
            Order p = obj as Order;
            if (this.orderid == p.orderid && this.MoneySum == p.MoneySum && Equals(this.client, p.client) && Equals(this.items, p.items))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public int orderid;
        public List<OrderDetails> items;
        public Client client;
        public double moneysum;
    }
    public class OrderService
    {
        public OrderService() { }
        public OrderService(List<Order> orders)
        {
            this.orders = orders;
            Orders = orders;
        }
        public List<Order> Orders { get; set; }
        public void Sort()
        {
            orders.Sort((x, y) => x.OrderID .CompareTo(y.OrderID ));
        }
        public void Add(Order order)
        {
            Console.WriteLine("添加订单"+order.OrderID);
            orders.Add(order);
        }
        public void Delete(int OrderID)
        {
            Console.WriteLine("删除订单" + OrderID);
            int i = 0;
            while (i < orders.Count)
            {
                if (orders[i].OrderID == OrderID)
                {
                    break;
                }
            }
            if (i < orders.Count)
            {
                orders.RemoveAt(i);
            }
            else
            {
                Console.WriteLine("订单删除失败，请检查输入的订单号!");
            }
        }
        public Order findid (int OrderID)//订单号实现查询
        {
            Console.WriteLine("寻找订单号为" + OrderID + "的订单");
            var result = orders.Where(m => m.OrderID == OrderID).FirstOrDefault();
            return (Order)result;
        }
        public Order findclient (Client client)//客户名实现查询
        {
            Console.WriteLine("寻找客户为" + client.Name + "的订单");
            var result = orders.Where(m => m.Client == client).FirstOrDefault();
            return (Order)result;
        }
        public void change(int OrderID, Order neworder)
        {
            Console.WriteLine("\n将订单" + OrderID + "修改为" + neworder.OrderID);
            int i = 0;
            while (i < orders.Count)
            {
                if (orders[i].OrderID == OrderID)
                {
                    break;
                }
            }
            if (i < orders.Count)
            {
                orders[i] = neworder;
            }
            else
            {
                Console.WriteLine("订单修改失败，请检查输入的订单号!");
            }
        }
        public void show()
        {
            Console.Write("当前存有的订单号：");
            foreach (Order order in orders)
            {
                Console.Write(order.OrderID + "  ");
            }
            Console.WriteLine();
        }
        public void export()
        {
            XmlSerializer xml = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream("Orders.xml", FileMode.Create))
            {
                xml.Serialize(fs, orders);
            }
            Console.WriteLine("\nSerialized as XML");
            Console.WriteLine(File.ReadAllText("Orders.xml"));
        }
        public void import()
        {
            XmlSerializer xml = new XmlSerializer(typeof(List<Order>));
            using (FileStream fs = new FileStream("Orders.xml", FileMode.Open))
            {
                orders = (List<Order>)xml.Deserialize(fs);
                Console.WriteLine("\nDeserialized from Orders.xml");
            }
        }
        public List<Order> orders = new List<Order>();
    }
    class Program
    {
        static void Main(string[] args)    //各个测试用例均在main函数内
        {
            Goods bread = new Goods("bread", 5.0);
            Goods milk = new Goods("milk", 3.0);
            Goods paper = new Goods("paper", 24.5);
            Goods chocolate = new Goods("chocolate", 9.9);
            Client xiao_ming = new Client("xiao_ming");
            Client xiao_hua = new Client("xiao_hua");
            OrderDetails orderdetail_1 = new OrderDetails(bread, 2);
            OrderDetails orderdetail_2 = new OrderDetails(milk, 3);
            OrderDetails orderdetail_3 = new OrderDetails(paper, 1);
            OrderDetails orderdetail_4 = new OrderDetails(chocolate, 2);
            List<OrderDetails> items_1 = new List<OrderDetails> { orderdetail_1, orderdetail_2 };
            List<OrderDetails> items_2 = new List<OrderDetails> { orderdetail_3, orderdetail_4 };
            Order order_1 = new Order(1, items_1, xiao_ming);
            Order order_2 = new Order(2, items_2, xiao_hua);
            OrderService orderService = new OrderService();
            orderService.Add(order_2);
            orderService.Add(order_1);
            orderService.Sort();
            orderService.show();
            orderService.Delete(1);
            orderService.show();
            Console.Write(orderService.findid(2));
            orderService.change(2, order_1);
            orderService.show();
            Console.Write(orderService.findclient(xiao_ming));
            orderService.Add(order_2);
            orderService.export();
            orderService.import();
        }
    }
}
